<template>
    <label class="jul-checkbox" :class="{ 'is-checkbox': isChecked }">
        <span class="jul-checkbox__input">
            <span class="jul-checkbox__inner"></span>
            <input
            type="checkbox"
            class="jul-checkbox__original"
            :name="name"
            v-model="model"
            :value="label">
        </span>
        <span class="jul-checkbox__label">
            <slot></slot>
            <template v-if="!$slots.default">{{label}}</template>
        </span>
    </label>
</template>

<script>
export default {
    name: 'JulCheckbox',
    inject: {
        CheckboxGroup: {
            default: ''
        }
    },
    computed: {
        isGroup() {
            return !!this.CheckboxGroup
        },
        model: {
            get() {
                return this.isGroup ? this.CheckboxGroup.value : this.value
            },
            set(value) {
                this.isGroup ? this.CheckboxGroup.$emit('input', value) : this.$emit('input', value)
            }
        },
        // 控制被选中时的样式状态
        // 如果是group包裹，判断label是否在model中
        // 如果没有group包裹，直接用model
        isChecked() {
            return this.isGroup ? this.model.includes(this.label) : this.model
        }
    },
    props: {
        value: {
            type: Boolean,
            default: false
        },
        label: {
            type: String,
            default: ''
        },
        name: {
            type: String,
            default: ''
        }
    }
}
</script>

<style lang="scss">
    .jul-checkbox {
        color: #606266;
        font-weight: 500;
        font-style: 14px;
        position: relative;
        cursor: pointer;
        display: inline-block;
        white-space: nowrap;
        user-select: none;
        margin-right: 30px;
        .jul-checkbox__input {
            white-space: nowrap;
            cursor: pointer;
            outline: none;
            display: inline-block;
            line-height: 1;
            position: relative;
            vertical-align: middle;
            .jul-checkbox__inner {
                border: 1px solid #dcdfe6;
                display: inline-block;
                width: 14px;
                height: 14px;
                background-color: #fff;
                position: relative;
                cursor: pointer;
                border-radius: 3px;
                box-sizing: border-box;
                z-index: 1;
                transition: border-color .25s cubic-bezier(.71,-.46,.29,1.46),background-color .25s cubic-bezier(.71,-.46,.29,1.46);
                &::after {
                    box-sizing: content-box;
                    border: 2px solid #fff;
                    height: 7px;
                    border-left: 0;
                    border-top: 0;
                    content: "";
                    position: absolute;
                    top: 1px;
                    left: 4px;
                    transform: rotate(45deg) scaleY(0);
                    width: 3px;
                    transition: transform .15s ease-in .05s;
                    transform-origin: center;
                }
            }
            .jul-checkbox__original {
                opacity: 0;
                outline: none;
                position: absolute;
                left: 10px;
                margin: 0;
                width: 0;
                height: 0;
                z-index: -1;
            }
        }
        .jul-checkbox__label {
            display: inline-block;
            padding-left: 10px;
            line-height: 19px;
            font-style: 14px;
        }
    }

    // 点击之后的样式
    .jul-checkbox.is-checkbox {
        .jul-checkbox__input {
            .jul-checkbox__inner {
                background-color: #409eff;
                border-color: #409eff;
                &::after {
                    transform: rotate(45deg) scaleY(1);
                }
            }
        }
        .jul-checkbox__label {
            color: #409eff;
        }
    }
</style>
